package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderSettingService;
import com.atguigu.vo.OrderSettingDateVo;
import com.qiniu.streaming.model.ActivityRecords;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void addExcel(List<String[]> strings) {
        long time = new Date().getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        //只能插入今天以后的预约信息
        strings.stream().filter(item->{
            try {
                item[0].replace("/","-");
                long time1 = sdf.parse(item[0]).getTime();
                return time1-time>0;
            } catch (ParseException e) {
                throw new RuntimeException();
            }
        }).forEach(s->{
            //根据日期查询
            OrderSetting orderSetting = orderSettingDao.selectDate(s[0]);
            if (orderSetting==null){
                orderSettingDao.insert(s[0],s[1]);
            }
            orderSettingDao.update(s[0],s[1]);
        });
    }


    //更新日期操作
    @Override
    public void update(String orderDate, Integer number) {
        //判断是第一次设置 还是进行更新操作
        OrderSetting orderSetting = orderSettingDao.selectDate(orderDate);
        if (orderSetting==null){
            //插入
            orderSettingDao.insert(orderDate,number.toString());
        }
        //更新
        orderSettingDao.update(orderDate,number.toString());
    }

    /*
    查询近一个月的预约计划
     */
    @Override
    public List<OrderSettingDateVo> selectDate(String date) {
        List<OrderSetting> orderSettings = orderSettingDao.selectDateList(date);
        List<OrderSettingDateVo> collect = orderSettings.stream().map(item -> {
            OrderSettingDateVo orderSettingDateVo = new OrderSettingDateVo();
            BeanUtils.copyProperties(item, orderSettingDateVo);
            orderSettingDateVo.setOrderDate(item.getOrderDate().getDate());
            return orderSettingDateVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /*
    更新预约信息
     */
    @Override
    public int updateReservations(Date orderDate) {
        return orderSettingDao.updateReservations(orderDate);
    }

    @Override
    public OrderSetting selectOrderDate(String orderDate) {
        return orderSettingDao.selectOrderDate(orderDate);
    }
}
